import { config ,baseApiUrl} from '../../config/cfg';
import {myTicketList} from '../../model/ticket/ticket';
import { getRequest, postRequest} from '../../utils/request';
const { delay } = require('../../utils/delay');
const app = getApp();

export const getMyTickets=(mobile)=> {
  // if (config.useMock) {
  //   return mockGetMyTickets(mobile);
  // }else{
    return realGetMyTickets(mobile);
  // }
 }

 function mockGetMyTickets(mobile){
  return delay().then(()=>{
    return myTicketList;
  });
 }

 function realGetMyTickets(mobile){
  // var params = {url:baseApiUrl+'/真实子级地址?mobile='+mobile};
  var params = {url:app.get_request_url("hmborder", "user"),data:{mobile:mobile}};
  return postRequest(params);
}


export const getTicketByOrderNo=(orderNo)=> {
  // if (config.useMock) {
  //   return mockGetTicketByOrderNo(orderNo);
  // }else{
    return realGetTicketByOrderNo(orderNo);
  // }
 }

 function mockGetTicketByOrderNo(orderNo){
  return delay().then(()=>{
    let ts = myTicketList.filter(item=>item.orderNo===orderNo);
    if(ts && ts.length>0){
      return ts[0];
    }else{
      return null;
    }
  });
 }

 function realGetTicketByOrderNo(orderNo){
  // var params = {url:baseApiUrl+'/真实子级地址?orderNo='+orderNo};
  var info = orderNo.split(':');
  var order = info[0];
  var groupId = info[1];
  var saleDate = info[2];
  var params = {url:app.get_request_url("ticket", "user"),data:{orderNo:order, groupId:groupId, saleDate:saleDate}};
  return postRequest(params);
}